<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - STL</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="css/main.css">
		<script src="js/three.min.js"></script>
		<script src="js/OrbitControls.js"></script>
		<script src="js/STLLoader.js"></script>
		<script src="js/stats.min.js"></script>
	</head>
	<body>
		<div id="info">
			<a href="https://songxp03.github.io/SimFun/SimFun" target="_blank" rel="noopener"> 换热器通道</a>
			by Octree via Three.js<br/>
			<script type="text/javascript" src="//rf.revolvermaps.com/0/0/3.js?i=58x4kn61rqm&amp;b=0&amp;s=40&amp;m=2&amp;cl=ffffff&amp;co=010020&amp;cd=aa0000&amp;v0=60&amp;v1=60&amp;r=1" async="async"></script>
			<script>
				var _hmt = _hmt || [];
				(function() {
				  var hm = document.createElement("script");
				  hm.src = "https://hm.baidu.com/hm.js?c92893cdabb11ad8ca139c33152761e2";
				  var s = document.getElementsByTagName("script")[0]; 
				  s.parentNode.insertBefore(hm, s);
				})();
			</script>
		</div>
		<script type="module">

			//import * as THREE from '../build/three.module.js';

			//import Stats from './jsm/libs/stats.module.js';

			//import { STLLoader } from './jsm/loaders/STLLoader.js';

			var container, stats;

			var camera, scene, renderer/*cameraTarget*/;

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
				camera.position.set( 3, 0.15, 3 );

				//cameraTarget = new THREE.Vector3( 0, - 0.25, 0 );

				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0x72645b );
				//scene.fog = new THREE.Fog( 0x72645b, 2, 15 );


				// Ground

				var plane = new THREE.Mesh(
					new THREE.PlaneBufferGeometry( 40, 40 ),
					new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
				);
				plane.rotation.x = - Math.PI / 2;
				plane.position.y = - 0.5;
				scene.add( plane );

				plane.receiveShadow = true;


				// ASCII file

				var loader = new THREE.STLLoader();
				loader.load( './models/stl/shell_and_tube_heat_exchanger-water.stl', function ( geometry ) {
					geometry.center();

					var material = new THREE.MeshPhongMaterial( { color: 0x00ff00, specular: 0x111111, shininess: 200 } );
					material.side=THREE.DoubleSide;
					var mesh = new THREE.Mesh( geometry, material );

					mesh.position.set( 0, -0.125, 0.4 );
					//mesh.rotation.set( 0, - Math.PI / 2, 0 );
					mesh.scale.set( 0.001, 0.001, 0.001 );

					mesh.castShadow = true;
					mesh.receiveShadow = true;

					scene.add( mesh );

				} );

				loader.load( './models/stl/shell_and_tube_heat_exchanger-air.stl', function ( geometry ) {
					geometry.center();

					var material = new THREE.MeshPhongMaterial( { color: 0xff0000, specular: 0x111111, shininess: 200 } );
					material.side=THREE.DoubleSide;
					var mesh = new THREE.Mesh( geometry, material );

					mesh.position.set( 0, -0.125, 0.8 );
					//mesh.rotation.set( 0, - Math.PI / 2, 0 );
					mesh.scale.set( 0.001, 0.001, 0.001 );

					mesh.castShadow = true;
					mesh.receiveShadow = true;

					scene.add( mesh );

				} );

				// Lights

				scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) );

				addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
				addShadowedLight( 0.5, 1, - 1, 0xffaa00, 1 );
				// renderer

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );

				renderer.gammaInput = true;
				renderer.gammaOutput = true;

				renderer.shadowMap.enabled = true;

				container.appendChild( renderer.domElement );

				// stats

				stats = new Stats();
				container.appendChild( stats.dom );

				//
				window.addEventListener( 'resize', onWindowResize, false );

				var controls = new THREE.OrbitControls( camera, renderer.domElement );
				controls.addEventListener( 'change', render );
			}

			function addShadowedLight( x, y, z, color, intensity ) {

				var directionalLight = new THREE.DirectionalLight( color, intensity );
				directionalLight.position.set( x, y, z );
				scene.add( directionalLight );

				directionalLight.castShadow = true;

				var d = 1;
				directionalLight.shadow.camera.left = - d;
				directionalLight.shadow.camera.right = d;
				directionalLight.shadow.camera.top = d;
				directionalLight.shadow.camera.bottom = - d;

				directionalLight.shadow.camera.near = 1;
				directionalLight.shadow.camera.far = 4;

				directionalLight.shadow.bias = - 0.002;

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			function render() {

				//var timer = Date.now() * 0.0005;

				//camera.position.x = Math.cos( timer ) * 3;
				//camera.position.z = Math.sin( timer ) * 3;

				//camera.lookAt( cameraTarget );

				renderer.render( scene, camera );

			}

		</script>
	</body>
</html>
